package com.zjitc.bookshop39.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author:徐莹
 * @Date:2025/04/14
 * 统一响应结果类
 */
@Data
public class ApiResponse<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 状态码
     */
    private Integer code;
    
    /**
     * 返回消息
     */
    private String message;
    
    /**
     * 返回数据
     */
    private T data;
    
    /**
     * 是否成功
     */
    private Boolean success;
    
    // 手动添加getter和setter方法，解决Lombok可能未生效的问题
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    // 添加int参数版本的setCode
    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }
    
    /**
     * 成功返回结果
     * @param data 返回数据
     * @return ResponseResult
     */
    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> result = new ApiResponse<>();
        result.setCode(200);
        result.setMessage("操作成功");
        result.setData(data);
        result.setSuccess(true);
        return result;
    }
    
    /**
     * 成功返回结果
     * @param message 返回消息
     * @param data 返回数据
     * @return ResponseResult
     */
    public static <T> ApiResponse<T> success(String message, T data) {
        ApiResponse<T> result = new ApiResponse<>();
        result.setCode(200);
        result.setMessage(message);
        result.setData(data);
        result.setSuccess(true);
        return result;
    }
    
    /**
     * 失败返回结果
     * @param message 错误消息
     * @return ResponseResult
     */
    public static <T> ApiResponse<T> error(String message) {
        ApiResponse<T> result = new ApiResponse<>();
        result.setCode(500);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
    
    /**
     * 失败返回结果
     * @param code 状态码
     * @param message 错误消息
     * @return ResponseResult
     */
    public static <T> ApiResponse<T> error(Integer code, String message) {
        ApiResponse<T> result = new ApiResponse<>();
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }
} 